package test.luin.pers.myapplication;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

public class BindServiceActivity extends AppCompatActivity {
    MyConnaction myConnaction;
    MyService2.MyBinder myBinder;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_bind_service);
    }

    public void bind(View view) {
        Intent in = new Intent(this, MyService2.class);
        if (myConnaction == null) {
            myConnaction = new MyConnaction();
        }
        bindService(in, myConnaction, BIND_AUTO_CREATE);
    }

    public void unbind(View view) {
        if (myConnaction != null) {
            unbindService(myConnaction);
            myConnaction = null;
        }
    }

    public void call(View view) {
        myBinder.callMehtod();
    }

    class MyConnaction implements ServiceConnection {

        @Override
        public void onServiceConnected(ComponentName componentName, IBinder iBinder) {
            myBinder = (MyService2.MyBinder) iBinder;
            Log.i("Service", "onServiceConnected()被调用，地址为"+myBinder.toString());
        }

        @Override
        public void onServiceDisconnected(ComponentName componentName) {

        }
    }

}
